package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.LiuhecaiTrendResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.SquareTextView;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class LXCNumberTrendAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<LiuhecaiTrendResponse> liuhecaiTrendResponseList;
    public LXCNumberTrendAdapter(Context context, ArrayList<LiuhecaiTrendResponse> liuhecaiTrendResponseList) {
        mContext = context;
        this.liuhecaiTrendResponseList = liuhecaiTrendResponseList;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.lxc_item2, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        String year = Utils.toYear(System.currentTimeMillis());
        if(liuhecaiTrendResponseList.get(position).getIssue_no().startsWith(year)){
            holder.no.setText(liuhecaiTrendResponseList.get(position).getIssue_no().substring(4));
        } else {
            holder.no.setText(liuhecaiTrendResponseList.get(position).getIssue_no());
        }
        String[] shuxiang = liuhecaiTrendResponseList.get(position).getPrize_num().split(",");
        holder.qiu1.setText(shuxiang[0]);
        holder.qiu2.setText(shuxiang[1]);
        holder.qiu3.setText(shuxiang[2]);
        holder.qiu4.setText(shuxiang[3]);
        holder.qiu5.setText(shuxiang[4]);
        holder.qiu6.setText(shuxiang[5]);
        holder.qiu7.setText(shuxiang[6]);
        holder.text1.setText(liuhecaiTrendResponseList.get(position).getAttached().getSx().get(0));
        holder.text2.setText(liuhecaiTrendResponseList.get(position).getAttached().getSx().get(1));
        holder.text3.setText(liuhecaiTrendResponseList.get(position).getAttached().getSx().get(2));
        holder.text4.setText(liuhecaiTrendResponseList.get(position).getAttached().getSx().get(3));
        holder.text5.setText(liuhecaiTrendResponseList.get(position).getAttached().getSx().get(4));
        holder.text6.setText(liuhecaiTrendResponseList.get(position).getAttached().getSx().get(5));
        holder.text7.setText(liuhecaiTrendResponseList.get(position).getAttached().getSx().get(6));
        setBack(holder.qiu1, liuhecaiTrendResponseList.get(position).getAttached().getColor().get(0));
        setBack(holder.qiu2, liuhecaiTrendResponseList.get(position).getAttached().getColor().get(1));
        setBack(holder.qiu3, liuhecaiTrendResponseList.get(position).getAttached().getColor().get(2));
        setBack(holder.qiu4, liuhecaiTrendResponseList.get(position).getAttached().getColor().get(3));
        setBack(holder.qiu5, liuhecaiTrendResponseList.get(position).getAttached().getColor().get(4));
        setBack(holder.qiu6, liuhecaiTrendResponseList.get(position).getAttached().getColor().get(5));
        setBack(holder.qiu7, liuhecaiTrendResponseList.get(position).getAttached().getColor().get(6));

        return view;
    }

    private void setBack(View view, String c){
        switch (c) {
            case "blue":
                view.setBackgroundResource(R.drawable.circle_bg_blue);
                break;
            case "red":
                view.setBackgroundResource(R.drawable.circle_bg_red);
                break;
            case "green":
                view.setBackgroundResource(R.drawable.circle_bg_green);
                break;
        }
    }

    @Override
    public int getCount() {
        return liuhecaiTrendResponseList.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }


    static class ViewHolder {
        @BindView(R.id.no)
        TextView no;
        @BindView(R.id.qiu1)
        SquareTextView qiu1;
        @BindView(R.id.text1)
        TextView text1;
        @BindView(R.id.qiu2)
        SquareTextView qiu2;
        @BindView(R.id.text2)
        TextView text2;
        @BindView(R.id.qiu3)
        SquareTextView qiu3;
        @BindView(R.id.text3)
        TextView text3;
        @BindView(R.id.qiu4)
        SquareTextView qiu4;
        @BindView(R.id.text4)
        TextView text4;
        @BindView(R.id.qiu5)
        SquareTextView qiu5;
        @BindView(R.id.text5)
        TextView text5;
        @BindView(R.id.qiu6)
        SquareTextView qiu6;
        @BindView(R.id.text6)
        TextView text6;
        @BindView(R.id.qiu7)
        SquareTextView qiu7;
        @BindView(R.id.text7)
        TextView text7;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
